package com.example.demo.error;

import com.example.demo.entity.ErrorInfo;
import com.example.demo.entity.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {
    public static final String DEFAULT_ERROR_VIEW="error";

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaulterrorHandler(HttpServletRequest request, Exception e){
       ModelAndView modelAndView=new ModelAndView();
       modelAndView.addObject("exception", e);
       modelAndView.addObject("url", request.getRequestURL());
       modelAndView.setViewName(DEFAULT_ERROR_VIEW);
       return modelAndView;
    }

    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ErrorInfo<String> jsonErrorHandler(HttpServletRequest request, MyException e){
        ErrorInfo<String> r=new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(ErrorInfo.ERROR);
        r.setData("Some Data");
        r.setUrl(request.getRequestURL().toString());
        return r;
    }
}

